package com.gmail.woodyc40.common;
import net.tridentsdk.Handler;
import net.tridentsdk.plugin.TridentPlugin;
import net.tridentsdk.util.TridentLogger;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
/**
* Access to sun.misc.Unsafe
*
* @author Pierre C
*/
public final class UnsafeTool {
/** Unsafe instance */
private static final Unsafe UNSAFE = hackUnsafe();
/** Prevent instantiation */
private UnsafeTool() {
}
/**
* Obtains the instance of unsafe
*
* <p>May not exist on certain platforms</p>
*
* @return the unsafe instance, if it exists
*/
private static Unsafe hackUnsafe() {
try {
Class<?> c = Class.forName("sun.misc.Unsafe");
Field field = c.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe) field.get(null);
} catch (ClassNotFoundException | NoSuchFieldException e) {
TridentLogger.error("Your platform does not support sun.misc.Unsafe");
Handler.forPlugins().disable(TridentPlugin.instance());
} catch (IllegalAccessException e) {
e.printStackTrace();
Handler.forPlugins().disable(TridentPlugin.instance());
}
return null;
}
/**
* Obtains the instance of unsafe
*
* @return the instance of unsafe
*/
public static Unsafe unsafe() {
return UNSAFE;
}
}